import axios from "axios";

const errTxt = "请求失败，网络状况不佳";

/**
 * TODO: 错误日志上报
 */
const http = axios.create({
  baseURL: "http://192.168.0.6:5555",
  // baseURL: 'http://127.0.0.1',
  timeout: 10000,
});

/**
 * 错误处理
 */
const errorHandler = (error) => {
  return Promise.reject(error);
};

/**
 * request 拦截器
 */
http.interceptors.request.use((config) => {
  config.params = config.params || {};
  config.data = config.data || {};

  return config;
}, errorHandler);

/**
 * response 拦截器
 */
http.interceptors.response.use(async (response) => {
  if (response.status === 200) {
    return Promise.resolve(response.data);
  }

  errorHandler(errTxt);
  return Promise.reject(new Error(errTxt));
}, errorHandler);

export default http;
